import { ref, computed } from 'vue';

function useTodo() {
  let title = ref('');
  let todoList = ref([{ name: '学习Vue3', done: false }]);

  function addTodo() {
    todoList.value.push({
      name: title.value,
    });
    title.value = '';
  }

  function clear() {
    todoList.value = todoList.value.filter((item) => !item.done);
  }

  // ------------------------------------------------------------------------------

  /** 全部数量 */
  let all = computed(() => {
    return todoList.value.length;
  });

  let active = computed(() => {
    return todoList.value.filter((item) => item.done).length;
  });

  let allDone = computed({
    get() {
      const filter = todoList.value.filter((item) => item.done);
      return filter.length && filter.length === todoList.value.length;
    },

    set(value) {
      todoList.value.forEach((item) => {
        item.done = value;
      });
    },
  });

  return {
    addTodo,
    clear,
    /** 全部数量 */
    all,
    active,
    allDone,
    todoList,
    title,
  };
}

export { useTodo };
